<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="本文通过微信提供的公众号文章调用接口，实现爬取公众号文章的功能">
<meta property="og:type" content="article">
<meta property="og:title" content="用python爬取微信公众号文章_Python_Jepson的博客-CSDN博客">
<meta property="og:url" content="http://example.com/2020/03/20/Pratical/%E7%94%A8python%E7%88%AC%E5%8F%96%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E6%96%87%E7%AB%A0_Python_Jepson%E7%9A%84%E5%8D%9A%E5%AE%A2-CSDN%E5%8D%9A%E5%AE%A2/index.html">
<meta property="og:site_name" content="ThWh&#39;s Blog">
<meta property="og:description" content="本文通过微信提供的公众号文章调用接口，实现爬取公众号文章的功能">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-03-19T21:56:37.040Z">
<meta property="article:modified_time" content="2020-05-26T13:09:04.111Z">
<meta property="article:author" content="ThWh">
<meta property="article:tag" content="Wechat">
<meta property="article:tag" content="Spider">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2020/03/20/Pratical/%E7%94%A8python%E7%88%AC%E5%8F%96%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E6%96%87%E7%AB%A0_Python_Jepson%E7%9A%84%E5%8D%9A%E5%AE%A2-CSDN%E5%8D%9A%E5%AE%A2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>用python爬取微信公众号文章_Python_Jepson的博客-CSDN博客 | ThWh's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">ThWh's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>About</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2020/03/20/Pratical/%E7%94%A8python%E7%88%AC%E5%8F%96%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E6%96%87%E7%AB%A0_Python_Jepson%E7%9A%84%E5%8D%9A%E5%AE%A2-CSDN%E5%8D%9A%E5%AE%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="ThWh">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ThWh's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          用python爬取微信公众号文章_Python_Jepson的博客-CSDN博客
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-03-20 05:56:37" itemprop="dateCreated datePublished" datetime="2020-03-20T05:56:37+08:00">2020-03-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-05-26 21:09:04" itemprop="dateModified" datetime="2020-05-26T21:09:04+08:00">2020-05-26</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Knowledges/" itemprop="url" rel="index"><span itemprop="name">Knowledges</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="本文通过微信提供的公众号文章调用接口，实现爬取公众号文章的功能"><a href="#本文通过微信提供的公众号文章调用接口，实现爬取公众号文章的功能" class="headerlink" title="本文通过微信提供的公众号文章调用接口，实现爬取公众号文章的功能"></a>本文通过微信提供的公众号文章调用接口，实现爬取公众号文章的功能</h4><a id="more"></a>
<p><strong>注意事项</strong><br>1.需要安装python selenium模块包，通过selenium中的webdriver驱动浏览器获取Cookie的方法、来达到登录的效果；<br>2.使用webdriver功能需要安装对应浏览器的驱动插件，我这里测试用的是谷歌浏览器：<br>google chrome版本为52.0.2743.6 ;<br>chromedriver版本为：V2.23<br>注意：谷歌浏览器版本和chromedriver需要对应，否则会导致启动时报错。【附：selenium之 chromedriver与chrome版本映射表<a target="_blank" rel="noopener" href="http://blog.csdn.net/huilan_same/article/details/51896672">更新至v2.30</a>）】<br>3.微信公众号登陆地址：<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/">https://mp.weixin.qq.com/</a><br>4.微信公众号文章接口地址可以在微信公众号后台中新建图文消息，超链接功能中获取：</p>


<p>5.搜索公众号名称  </p>


<p>搜索可以获取所有相关的公众号信息，不过我这里只取第一个做测试，其他的有兴趣的也可以全部获取。</p>
<p>6.获取要爬取的公众号的fakeid</p>


<p>7.选定要爬取的公众号，获取文章接口地址</p>


<p>8.文章列表翻页及内容获取</p>


<p>9.详细细节查资料研究吧，可参考崔大神的文章，本文参照原文进行整理说明（<a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s?__biz=MzI5NDY1MjQzNA==&mid=2247483970&idx=1&sn=cde40462d2346ded8e8c11ab4442bbab&chksm=ec5edd3fdb2954299e5b4736b3729014d4853e50e643de06640ba3af370753db069667511db1&mpshare=1&scene=1&srcid=0612suzxGJXTmoak9i81rRSZ&pass_ticket=YsJz0pUrK8Yj6XuoyHfGbfjFAgRZ9wHQMTLCnfaYLlQGaOXangzh2LWgrfB8lf76#rd">原文地址</a>）</p>
<p><strong>完整代码</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="comment">#微信公众号账号</span></span><br><span class="line">user=<span class="string">&quot;你的公众号账号&quot;</span></span><br><span class="line"><span class="comment">#公众号密码</span></span><br><span class="line">password=<span class="string">&quot;你的公众号密码&quot;</span></span><br><span class="line"><span class="comment">#设置要爬取的公众号列表</span></span><br><span class="line">gzlist=[<span class="string">&#x27;要爬取的公众号名字&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#登录微信公众号，获取登录之后的cookies信息，并保存到本地文本中</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">weChat_login</span>():</span></span><br><span class="line">    <span class="comment">#定义一个空的字典，存放cookies内容</span></span><br><span class="line">    post=&#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#用webdriver启动谷歌浏览器</span></span><br><span class="line">    print(<span class="string">&quot;启动浏览器，打开微信公众号登录界面&quot;</span>)</span><br><span class="line">    driver = webdriver.Chrome(executable_path=<span class="string">&#x27;C:\chromedriver.exe&#x27;</span>)</span><br><span class="line">    <span class="comment">#打开微信公众号登录页面</span></span><br><span class="line">    driver.get(<span class="string">&#x27;https://mp.weixin.qq.com/&#x27;</span>)</span><br><span class="line">    <span class="comment">#等待5秒钟</span></span><br><span class="line">    time.sleep(<span class="number">5</span>)</span><br><span class="line">    print(<span class="string">&quot;正在输入微信公众号登录账号和密码......&quot;</span>)</span><br><span class="line">    <span class="comment">#清空账号框中的内容</span></span><br><span class="line">    driver.find_element_by_xpath(<span class="string">&quot;./*//input[@id=&#x27;account&#x27;]&quot;</span>).clear()</span><br><span class="line">    <span class="comment">#自动填入登录用户名</span></span><br><span class="line">    driver.find_element_by_xpath(<span class="string">&quot;./*//input[@id=&#x27;account&#x27;]&quot;</span>).send_keys(user)</span><br><span class="line">    <span class="comment">#清空密码框中的内容</span></span><br><span class="line">    driver.find_element_by_xpath(<span class="string">&quot;./*//input[@id=&#x27;pwd&#x27;]&quot;</span>).clear()</span><br><span class="line">    <span class="comment">#自动填入登录密码</span></span><br><span class="line">    driver.find_element_by_xpath(<span class="string">&quot;./*//input[@id=&#x27;pwd&#x27;]&quot;</span>).send_keys(password)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 在自动输完密码之后需要手动点一下记住我</span></span><br><span class="line">    print(<span class="string">&quot;请在登录界面点击:记住账号&quot;</span>)</span><br><span class="line">    time.sleep(<span class="number">10</span>)</span><br><span class="line">    <span class="comment">#自动点击登录按钮进行登录</span></span><br><span class="line">    driver.find_element_by_xpath(<span class="string">&quot;./*//a[@id=&#x27;loginBt&#x27;]&quot;</span>).click()</span><br><span class="line">    <span class="comment"># 拿手机扫二维码！</span></span><br><span class="line">    print(<span class="string">&quot;请拿手机扫码二维码登录公众号&quot;</span>)</span><br><span class="line">    time.sleep(<span class="number">20</span>)</span><br><span class="line">    print(<span class="string">&quot;登录成功&quot;</span>)</span><br><span class="line">    <span class="comment">#重新载入公众号登录页，登录之后会显示公众号后台首页，从这个返回内容中获取cookies信息</span></span><br><span class="line">    driver.get(<span class="string">&#x27;https://mp.weixin.qq.com/&#x27;</span>)</span><br><span class="line">    <span class="comment">#获取cookies</span></span><br><span class="line">    cookie_items = driver.get_cookies()</span><br><span class="line"></span><br><span class="line">    <span class="comment">#获取到的cookies是列表形式，将cookies转成json形式并存入本地名为cookie的文本中</span></span><br><span class="line">    <span class="keyword">for</span> cookie_item <span class="keyword">in</span> cookie_items:</span><br><span class="line">        post[cookie_item[<span class="string">&#x27;name&#x27;</span>]] = cookie_item[<span class="string">&#x27;value&#x27;</span>]</span><br><span class="line">    cookie_str = json.dumps(post)</span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">&#x27;cookie.txt&#x27;</span>, <span class="string">&#x27;w+&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        f.write(cookie_str)</span><br><span class="line">    print(<span class="string">&quot;cookies信息已保存到本地&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#爬取微信公众号文章，并存在本地文本中</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_content</span>(<span class="params">query</span>):</span></span><br><span class="line">    <span class="comment">#query为要爬取的公众号名称</span></span><br><span class="line">    <span class="comment">#公众号主页</span></span><br><span class="line">    url = <span class="string">&#x27;https://mp.weixin.qq.com&#x27;</span></span><br><span class="line">    <span class="comment">#设置headers</span></span><br><span class="line">    header = &#123;</span><br><span class="line">        <span class="string">&quot;HOST&quot;</span>: <span class="string">&quot;mp.weixin.qq.com&quot;</span>,</span><br><span class="line">        <span class="string">&quot;User-Agent&quot;</span>: <span class="string">&quot;Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">#读取上一步获取到的cookies</span></span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">&#x27;cookie.txt&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        cookie = f.read()</span><br><span class="line">    cookies = json.loads(cookie)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#登录之后的微信公众号首页url变化为：https://mp.weixin.qq.com/cgi-bin/home?t=home/index&amp;lang=zh_CN&amp;token=1849751598，从这里获取token信息</span></span><br><span class="line">    response = requests.get(url=url, cookies=cookies)</span><br><span class="line">    token = re.findall(<span class="string">r&#x27;token=(\d+)&#x27;</span>, str(response.url))[<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line">    <span class="comment">#搜索微信公众号的接口地址</span></span><br><span class="line">    search_url = <span class="string">&#x27;https://mp.weixin.qq.com/cgi-bin/searchbiz?&#x27;</span></span><br><span class="line">    <span class="comment">#搜索微信公众号接口需要传入的参数，有三个变量：微信公众号token、随机数random、搜索的微信公众号名字</span></span><br><span class="line">    query_id = &#123;</span><br><span class="line">        <span class="string">&#x27;action&#x27;</span>: <span class="string">&#x27;search_biz&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;token&#x27;</span> : token,</span><br><span class="line">        <span class="string">&#x27;lang&#x27;</span>: <span class="string">&#x27;zh_CN&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;f&#x27;</span>: <span class="string">&#x27;json&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;ajax&#x27;</span>: <span class="string">&#x27;1&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;random&#x27;</span>: random.random(),</span><br><span class="line">        <span class="string">&#x27;query&#x27;</span>: query,</span><br><span class="line">        <span class="string">&#x27;begin&#x27;</span>: <span class="string">&#x27;0&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;count&#x27;</span>: <span class="string">&#x27;5&#x27;</span></span><br><span class="line">        &#125;  </span><br><span class="line">    <span class="comment">#打开搜索微信公众号接口地址，需要传入相关参数信息如：cookies、params、headers</span></span><br><span class="line">    search_response = requests.get(search_url, cookies=cookies, headers=header, params=query_id)</span><br><span class="line">    <span class="comment">#取搜索结果中的第一个公众号</span></span><br><span class="line">    lists = search_response.json().get(<span class="string">&#x27;list&#x27;</span>)[<span class="number">0</span>]</span><br><span class="line">    <span class="comment">#获取这个公众号的fakeid，后面爬取公众号文章需要此字段</span></span><br><span class="line">    fakeid = lists.get(<span class="string">&#x27;fakeid&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#微信公众号文章接口地址</span></span><br><span class="line">    appmsg_url = <span class="string">&#x27;https://mp.weixin.qq.com/cgi-bin/appmsg?&#x27;</span></span><br><span class="line">    <span class="comment">#搜索文章需要传入几个参数：登录的公众号token、要爬取文章的公众号fakeid、随机数random</span></span><br><span class="line">    query_id_data = &#123;</span><br><span class="line">        <span class="string">&#x27;token&#x27;</span>: token,</span><br><span class="line">        <span class="string">&#x27;lang&#x27;</span>: <span class="string">&#x27;zh_CN&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;f&#x27;</span>: <span class="string">&#x27;json&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;ajax&#x27;</span>: <span class="string">&#x27;1&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;random&#x27;</span>: random.random(),</span><br><span class="line">        <span class="string">&#x27;action&#x27;</span>: <span class="string">&#x27;list_ex&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;begin&#x27;</span>: <span class="string">&#x27;0&#x27;</span>,<span class="comment">#不同页，此参数变化，变化规则为每页加5</span></span><br><span class="line">        <span class="string">&#x27;count&#x27;</span>: <span class="string">&#x27;5&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;query&#x27;</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;fakeid&#x27;</span>: fakeid,</span><br><span class="line">        <span class="string">&#x27;type&#x27;</span>: <span class="string">&#x27;9&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    <span class="comment">#打开搜索的微信公众号文章列表页</span></span><br><span class="line">    appmsg_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)</span><br><span class="line">    <span class="comment">#获取文章总数</span></span><br><span class="line">    max_num = appmsg_response.json().get(<span class="string">&#x27;app_msg_cnt&#x27;</span>)</span><br><span class="line">    <span class="comment">#每页至少有5条，获取文章总的页数，爬取时需要分页爬</span></span><br><span class="line">    num = int(int(max_num) / <span class="number">5</span>)</span><br><span class="line">    <span class="comment">#起始页begin参数，往后每页加5</span></span><br><span class="line">    begin = <span class="number">0</span></span><br><span class="line">    <span class="keyword">while</span> num + <span class="number">1</span> &gt; <span class="number">0</span> :</span><br><span class="line">        query_id_data = &#123;</span><br><span class="line">            <span class="string">&#x27;token&#x27;</span>: token,</span><br><span class="line">            <span class="string">&#x27;lang&#x27;</span>: <span class="string">&#x27;zh_CN&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;f&#x27;</span>: <span class="string">&#x27;json&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;ajax&#x27;</span>: <span class="string">&#x27;1&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;random&#x27;</span>: random.random(),</span><br><span class="line">            <span class="string">&#x27;action&#x27;</span>: <span class="string">&#x27;list_ex&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;begin&#x27;</span>: <span class="string">&#x27;&#123;&#125;&#x27;</span>.format(str(begin)),</span><br><span class="line">            <span class="string">&#x27;count&#x27;</span>: <span class="string">&#x27;5&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;query&#x27;</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;fakeid&#x27;</span>: fakeid,</span><br><span class="line">            <span class="string">&#x27;type&#x27;</span>: <span class="string">&#x27;9&#x27;</span></span><br><span class="line">            &#125;</span><br><span class="line">        print(<span class="string">&#x27;正在翻页：--------------&#x27;</span>,begin)</span><br><span class="line"></span><br><span class="line">        <span class="comment">#获取每一页文章的标题和链接地址，并写入本地文本中</span></span><br><span class="line">        query_fakeid_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)</span><br><span class="line">        fakeid_list = query_fakeid_response.json().get(<span class="string">&#x27;app_msg_list&#x27;</span>)</span><br><span class="line">        <span class="keyword">for</span> item <span class="keyword">in</span> fakeid_list:</span><br><span class="line">            content_link=item.get(<span class="string">&#x27;link&#x27;</span>)</span><br><span class="line">            content_title=item.get(<span class="string">&#x27;title&#x27;</span>)</span><br><span class="line">            fileName=query+<span class="string">&#x27;.txt&#x27;</span></span><br><span class="line">            <span class="keyword">with</span> open(fileName,<span class="string">&#x27;a&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>) <span class="keyword">as</span> fh:</span><br><span class="line">                fh.write(content_title+<span class="string">&quot;:\n&quot;</span>+content_link+<span class="string">&quot;\n&quot;</span>)</span><br><span class="line">        num -= <span class="number">1</span></span><br><span class="line">        begin = int(begin)</span><br><span class="line">        begin+=<span class="number">5</span></span><br><span class="line">        time.sleep(<span class="number">2</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__==<span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="comment">#登录微信公众号，获取登录之后的cookies信息，并保存到本地文本中</span></span><br><span class="line">        weChat_login()</span><br><span class="line">        <span class="comment">#登录之后，通过微信公众号后台提供的微信公众号文章接口爬取文章</span></span><br><span class="line">        <span class="keyword">for</span> query <span class="keyword">in</span> gzlist:</span><br><span class="line">            <span class="comment">#爬取微信公众号文章，并存在本地文本中</span></span><br><span class="line">            print(<span class="string">&quot;开始爬取公众号：&quot;</span>+query)</span><br><span class="line">            get_content(query)</span><br><span class="line">            print(<span class="string">&quot;爬取完成&quot;</span>)</span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        print(str(e))</span><br></pre></td></tr></table></figure>

<p>版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。</p>
<p>本文链接：<a target="_blank" rel="noopener" href="https://blog.csdn.net/d1240673769/article/details/75907152">https://blog.csdn.net/d1240673769/article/details/75907152</a></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Wechat/" rel="tag"># Wechat</a>
              <a href="/tags/Spider/" rel="tag"># Spider</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/03/19/CTF/web_wp/baby-web/" rel="prev" title="baby_web">
      <i class="fa fa-chevron-left"></i> baby_web
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/03/20/CTF/web_wp/Training-WWW-Robots/" rel="next" title="Training-WWW-Robots">
      Training-WWW-Robots <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9C%AC%E6%96%87%E9%80%9A%E8%BF%87%E5%BE%AE%E4%BF%A1%E6%8F%90%E4%BE%9B%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7%E6%96%87%E7%AB%A0%E8%B0%83%E7%94%A8%E6%8E%A5%E5%8F%A3%EF%BC%8C%E5%AE%9E%E7%8E%B0%E7%88%AC%E5%8F%96%E5%85%AC%E4%BC%97%E5%8F%B7%E6%96%87%E7%AB%A0%E7%9A%84%E5%8A%9F%E8%83%BD"><span class="nav-number">1.</span> <span class="nav-text">本文通过微信提供的公众号文章调用接口，实现爬取公众号文章的功能</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">ThWh</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">13</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">2</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ThWh</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
